using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace game
{
    public class ShooterGame : Microsoft.Xna.Framework.Game
    {
        public Effect debugEffect;

        public static ShooterGame Instance { get { return instance; } }
        private static ShooterGame instance = null;

        public GraphicsDeviceManager GraphicsDeviceManager { get { return graphicsMgr; } }
        public SpriteBatch SpriteBatch { get { return spriteBatch; } }

        public Random Random { get; set; }

        GraphicsDeviceManager graphicsMgr;
        SpriteBatch spriteBatch;

        ScreenManager screenMgr;
        GameScreen gameScreen;

        public BasicPrimitives Primitives { get; protected set; }
        public BasicEffect BasicEffect { get; protected set; }

        public ShooterGame()
        {
            Random = new Random();
            graphicsMgr = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Components.Add(new FpsMeter(this));
            instance = this;
        }

        protected override void Initialize()
        {
            graphicsMgr.PreferredBackBufferWidth = 800;
            graphicsMgr.PreferredBackBufferHeight = 450;
            graphicsMgr.IsFullScreen = true;
            graphicsMgr.SynchronizeWithVerticalRetrace = false;
            //graphicsMgr.PreferMultiSampling = true;
            graphicsMgr.ApplyChanges();
            IsFixedTimeStep = false; // = true; so it works on Anna's computer :P
            Window.Title = "SpaceShooter";
            screenMgr = new ScreenManager();
            Text2D.Initialize(this);
            
            Primitives = new BasicPrimitives(GraphicsDevice);
            BasicEffect = new BasicEffect(GraphicsDevice, null);
            //Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2,
            //    GraphicsDevice.Viewport.Height / 2);

            BoundingSphereRenderer.InitializeGraphics(GraphicsDevice, 10);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            Content = new ContentManager(Services, "Content");
            spriteBatch = new SpriteBatch(GraphicsDevice);

            gameScreen = new GameScreen();
            screenMgr.AddScreen(gameScreen, false);
            screenMgr.SetActiveScreen(gameScreen);
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            Input.Update();

            screenMgr.UpdateScreen(gameTime);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            screenMgr.DrawScreen(gameTime);
            
            base.Draw(gameTime);
        }
    }
}
